package top.fwpsl.netty.rpc;

import top.fwpsl.netty.rpc.rpc.api.HelloNetty;
import top.fwpsl.netty.rpc.rpc.api.HelloRPC;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * @auther: 风戏fw
 * @date: 2021/2/7
 * @description:
 */
public class TestNettyRpc {

    public static void main(String[] args) {

        // 消费方不需要知道底层的网络实现细节，就像调用本地方法一样成功发起了两次远程调用。

        // 第一次远程调用
        HelloNetty helloNetty = (HelloNetty) NettyRpcProxy.create(HelloNetty.class);
        System.out.println(helloNetty.hello());

        // 第二次远程调用
        HelloRPC helloRPC = (HelloRPC) NettyRpcProxy.create(HelloRPC.class);
        System.out.println(helloRPC.hello("RPC"));
        System.out.println(helloRPC.hello2("RPC", 20));

        Executor executor = Executors.newCachedThreadPool();


        long start = System.currentTimeMillis();
        for (int i = 0; i < 500; i++) {
            executor.execute(() -> {
                System.out.println(Thread.currentThread().getName() + "\t" + helloNetty.hello());
            });
            executor.execute(() -> {
                System.out.println(Thread.currentThread().getName() + "\t" + helloRPC.hello("RPC"));
            });
            executor.execute(() -> {
                System.out.println(Thread.currentThread().getName() + "\t" + helloRPC.hello2("RPC", 20));
            });
        }

        System.out.println(">>>>>>>>>>>>>>>>>>" + (System.currentTimeMillis() - start));
    }
}
